fix FileIO openTempFile on Windows
authorJoey Hess <joeyh@joeyh.name>
Thu, 30 Jan 2025 18:23:00 +0000 (14:23 -0400)
committerJoey Hess <joeyh@joeyh.name>
Thu, 30 Jan 2025 18:31:01 +0000 (14:31 -0400)
When an UNC-style path is passed into openTempFile, the returned file
starts with that same style of path. Which can cause problems, eg piping
that filename to git failed. So, convert the output filename to be
relative to the input temp directory.

Utility/FileIO.hs

index 4b12b2ba0ed8fa7fd18a54926af8fc0984f25e76..6a220259634ae111e54305cc4ddb4986c9eb5d22 100644 (file)
@@ -35,7 +35,9 @@ import System.File.OsPath
 -- https://github.com/haskell/file-io/issues/39
 import Utility.Path.Windows
 import Utility.OsPath
+import System.OsPath
 import System.IO (IO, Handle, IOMode)
+import Prelude (return)
 import qualified System.File.OsPath as O
 import qualified Data.ByteString as B
 import qualified Data.ByteString.Lazy as L
@@ -84,7 +86,10 @@ appendFile' f b = do
 openTempFile :: OsPath -> OsPath -> IO (OsPath, Handle)
 openTempFile p s = do
        p' <- toOsPath <$> convertToWindowsNativeNamespace (fromOsPath p)
-       O.openTempFile p' s
+       (t, h) <- O.openTempFile p' s
+       -- Avoid returning mangled path from convertToWindowsNativeNamespace
+       let t' = p </> takeFileName t
+       return (t', h)
 #endif
 
 #else